﻿# Create your views here.
from django.template.loader import get_template
from django.template import Context,RequestContext
from django.http import HttpResponse
import datetime
import traceback,sys
import urllib
#-*- encoding: iso8859-1 -*-
import urllib2
#biblioteca para requisies http
import cookielib
#biblioteca para tratar de cookies

#Ajax new implementation
from django.utils import simplejson
from time import strptime
from datetime import datetime, timedelta

from AIB_Login.views import userInfo


def get_time_diference(request):
	date_now = datetime.now() - timedelta(minutes=(0)) #gmtoffset
	timeTuple1 = strptime("2010-07-26 10:00","%Y-%m-%d %H:%M")
	dt_obj = datetime(*timeTuple1[0:6])
	time_difference = date_now - dt_obj
	return HttpResponse(simplejson.dumps({'time_diff':str( int((time_difference.days*86400+time_difference.seconds)/3600.0) )}), mimetype='application/json; charset=UTF-8')
	#return time_difference

def get_content(request):
	try:
		template = request.POST['template']
		if template.find("/")>-1:
			source,cj = get_html_source(template)
			html = source
		else:			
			t = get_template(template)
			c = RequestContext(request)
			html = t.render(c)
		return HttpResponse(html)	
	except:
		exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
		traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,
						  limit=2, file=sys.stdout) 		
	
	#return render_to_response('middle_content_logout.html')
	
def get_content_html(request,template_name):
	try:
		t = get_template(template_name)
		c = RequestContext(request)
		html = t.render(c)
		return HttpResponse(html)	
	except:
		exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
		traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,
						  limit=2, file=sys.stdout) 	 		
	
	#return render_to_response('middle_content_logout.html')

def get_middle(request,template_name):
	try:
		t = get_template('middle_content_url.html')
		#c = RequestContext(request,{"template_name":'registration/login.html'})
		c = RequestContext(request,{'template_name':template_name})
		html = t.render(c)
		return HttpResponse(html)	
	except:
		exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
		traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,
						  limit=2, file=sys.stdout) 	
		
def get_middle_eval(request,call_dict=None,extra_get=None):
	try:
		if call_dict:	
			if extra_get:
				extra_dict = eval("%s('%s')" % (call_dict,extra_get))
			else:				
				extra_dict = eval('%s(request)' % (call_dict))
		#print "'template_name'=%s" % (extra_dict['template_name'])
		t = get_template('middle_content_url.html')
		c = RequestContext(request,extra_dict)
		html = t.render(c)
		return HttpResponse(html)	
	except:
		exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
		traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,
						  limit=2, file=sys.stdout) 			
	
def get_two_columns(request,extra_get=None,extra_dict=None,call_dict=None):
	try:
		if call_dict:
			if extra_get:
				extra_dict = eval("%s('%s')" % (call_dict,extra_get))
			else:				
				extra_dict = eval('%s()' % (call_dict))
		#print "'template_name'=%s" % (extra_dict['template_name'])
		t = get_template('two_columns_url.html')
		#c = RequestContext(request,{"template_name":'registration/login.html'})
		#c = RequestContext(request,{'template_name':template_name})		
		if extra_get != None:
			#print "extra_get=%s" % (extra_get)
			extra_dict['extra_get'] = extra_get #adicionando extra_get
		c = RequestContext(request,extra_dict)
		html = t.render(c)
		return HttpResponse(html)	
	except:
		exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
		traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,
						  limit=2, file=sys.stdout) 
